package se.mbaeumer.tinydbc.client.console;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import se.mbaeumer.tinydbc.core.dbclient.AbstractDBClient;
import se.mbaeumer.tinydbc.core.dbclient.MySQLDBClient;
import se.mbaeumer.tinydbc.core.main.TinyDBClient;
import se.mbaeumer.tinydbc.core.server.DatabaseServer;

public class TinyDBClientConsole {
	private TinyDBClient tinydbclient = TinyDBClient.getInstance();
	
	/*TODO: Add status*/	
	/*TODO: Add info*/	
	/*TODO: Add about*/
	/*TODO: Use TinyDBClient, deprecate currentServer*/
	/*TODO: Formatting -> \n before some sysouts*/
	

	public TinyDBClientConsole(){};
	public static void main(String[] args) {
		TinyDBClientConsole tiny = new TinyDBClientConsole();
		/*
		if (tiny.getTinyDBClient().connectToHSQLDB()==1);{
			tiny.getTinyDBClient().disconnect();
		}		
		System.exit(0);
		*/
		tiny.showGeneralInfo();
		tiny.showMainMenu();
		String option = tiny.getUserInputForMain();

		if (option.equals("1")) {
			tiny.showServerCreationForm();
		} else if (option.equals("2")) {
			tiny.showDatabaseCreationForm();
		} else if (option.equals("3")) {
			tiny.exit();
		} else {
			tiny.exit();
		}

	}

	public void showMainMenu() {
		System.out.println("Main menu");
		System.out.println("----------");
		System.out.println("(1) Create a server");
		// System.out.println("(2) Select a server");
		// System.out.println("(3) View all existing servers");
		System.out.println("(2) Create a database (with all access granted to root)");
		System.out.println("(3) Exit");
		// System.out.println("(7) About");

		this.showStatus();
	}
	
	private void showGeneralInfo(){
		System.out.println("TinyDBClient Console client");
		System.out.println("NOTE: This client was originally developed for test purposes and does not provide full functionality. ");
	}

	private void showStatus() {
		System.out.println();
		System.out.println("-------------------");
		System.out.println("Status:");
		String currentServerInfo = "none";
		if (this.getTinyDBClient().getCurrentServer() != null){
			currentServerInfo = this.getTinyDBClient().getCurrentServer().getServerName() + "on " + this.getTinyDBClient().getCurrentServer().getServerAddress();
		}
		this.getTinyDBClient().getCurrentServer().getServerName();
		System.out.println("Current server: " + currentServerInfo);
		System.out.println("-------------------");
	}

	private void exit() {
		System.out.println("Shutting down...");
		System.exit(0);
	}

	private String getUserInputForMain() {
		System.out.println();
		BufferedReader console = new BufferedReader(new InputStreamReader(System.in));
		System.out.print("Please select an option: ");

		String option = null;
		try {
			option = console.readLine();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return option;
	}

	private void showServerCreationForm() {
		String port = "0";
		String username = "root";
		BufferedReader console = new BufferedReader(new InputStreamReader(
				System.in));
		String serverName = null;
		String serverAddress = null;
		String password = null;
		String isDefault = null;
		try {
			System.out.print("Servername: ");
			serverName = console.readLine();
			System.out.print("Address: ");
			serverAddress = console.readLine();
			System.out.print("Password for root: ");
			password = console.readLine();
			System.out.print("Default? (y): : ");
			isDefault = console.readLine();
			if (isDefault == null) {
				isDefault = "";
			}
		} catch (IOException e) {
			e.printStackTrace();
		}

		System.out.println();
		System.out.println("Creating database server...");

		this.getTinyDBClient().createServer(serverName, serverAddress, Integer.parseInt(port), username, password, isDefault.equals("y"));		
	}

	private void showDatabaseCreationForm() {
		String password = null;
		AbstractDBClient client = null;
		BufferedReader console = new BufferedReader(new InputStreamReader(
				System.in));

		if (this.getTinyDBClient().getCurrentServer() == null) {
			String serverName = "temp";
			String serverAddress = "localhost";
			String port = "0";
			String username = "root";

			/*currentServer = new DatabaseServer(serverName, serverAddress,
					Integer.parseInt(port));
			currentServer.setUsername(username);
			currentServer.setPassword(password);
			currentServer.setDefault(true);
			*/
			this.getTinyDBClient().createServer(serverName, serverAddress, Integer.parseInt(port), username, password, true);

			client = new MySQLDBClient(this.getTinyDBClient().getCurrentServer().getServerAddress(),
					this.getTinyDBClient().getCurrentServer().getUsername(), password);

			while (client.connect() == 0) {
				try {
					System.out.print("Password for root: ");
					password = console.readLine();
					this.getTinyDBClient().getCurrentServer().setPassword(password);
					client.setPassword(password);
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}

		String databaseName = "";
		while ((databaseName.equals("")) || (client.existsDatabase(databaseName))) {
			try {
				System.out.print("Database name: ");
				databaseName = console.readLine();
				databaseName.trim();				
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		System.out.println("All access will be granted to root on " +databaseName);
		
		this.getTinyDBClient().getCurrentServer().getDatabases().add(client);
		client.createDatabase(databaseName);
		client.grantRights(databaseName);
	}

	public TinyDBClient getTinyDBClient() {
		return tinydbclient;
	}

	public void setTinyDBClient(TinyDBClient tinydbclient) {
		this.tinydbclient = tinydbclient;
	}
}
